package com.hz.supermanage.dao;

import java.util.*;

import javax.servlet.http.HttpServletRequest;

import com.hz.framework.dao.CommonDao;
import com.hz.supermanage.bean.*;

public class ProgramaDao extends CommonDao{
	 /**
     * 返回节点的类型
     * @param type
     * @return
     */
    public String getNodeIcon(String type) {
    	System.out.println(type);
        if (Constants.NODE_TYPE_ORG.equals(type)) {
            return "/parts/images/img/xp4/vr2.gif";
        } else if (Constants.NODE_TYPE_DEPT.equals(type)) {
            return "/parts/images/img/xp4/vr5.gif";
        } else if (Constants.NODE_TYPE_POS.equals(type)) {
            return "/parts/images/img/xp4/sim.gif";
        }
        return "";
    }
	
	/**
     * 获得指定的权限栏目
     * @param primaryKey
     * @return
     * @throws Exception
     */
    public List getProNode(String primaryKey) throws Exception {
    	String hql="from HzsysPrograma where parentid ="+primaryKey+" and prostate='" + Constants.NODE_STATE_ACTIVE + "'  order by CONVERT(proorder ,SIGNED) asc  ";
        return this.selHqlList(hql);
    }
    
	/**
     * 节点是否有子存在
     * @param nodeId
     * @return
     */
    public boolean isHasChild(String nodeId)  {
    	String hql = "select count(*) from HzsysPrograma where parentid='" + nodeId + "' and prostate='" + Constants.NODE_STATE_ACTIVE + "'";
        System.out.println("isHasChild:"+hql);
        return Integer.valueOf(this.selHqlObj(hql).toString()) != 0;
    }
    
    /**
     * 是否有根节点
     * @return
     * @throws ConnectionException
     * @throws EntityOperationException
     */
    public boolean isHasRoot()  {
        String hql = "select count(*) as cu from HzsysPrograma where parentid is null and prostate='" + Constants.NODE_STATE_ACTIVE + "'";
        System.out.println("isHasRoot:"+hql);
        return Integer.valueOf(this.selHqlObj(hql).toString()) != 0;
    }
   
    /**
     * 获得子的集合
     * @param uuid
     * @return
     */
    public Collection getChildNodes(String parentUuid)  {
    	String hql="from HzsysPrograma where parentid ="+parentUuid+" and prostate='" + Constants.NODE_STATE_ACTIVE + "' order by CONVERT(proorder ,SIGNED) asc   ";
        return this.selHqlList(hql);
    }
    /**
     * 根据唯一键查询人员信息表
     * @param uuid
     * @return Map
     */
    public Map getPersonSelect(String uuid)  {
    	String hql = "select new map(p.personid as personid, p.loginid as loginid, p.personname as personname, " +
    				"p.orgid as orgid, dd.depname as deptid, dp.depname as posid, p.sex as sex, " +
    				"p.remark as remark, p.perdrder as perdrder, p.roleid as roleid, p.gramaid as gramaid) " +
    				" from Hzsyspersonnel p " +
    				"left join p.posid dp left join p.deptid as dd where p.personid ='" + uuid + "'";
        System.out.println("getPersonSelect:"+hql);
        return (Map)this.selHqlObj(hql);
    }
    
    /**
     * 根据唯一键查询机构名称
     * @param orgid
     * @return Object
     */
    public Object getOrgNameSelect(String orgid)  {
    	String hql = "select depname as depname from Hzsysorganization where depid = '"+ orgid+"'";
        System.out.println("getOrgNameSelect:"+hql);
        return this.selHqlObj(hql);
    }
    
    /**
     * 查询所有角色信息
     * @return 
     */
    public void RqsetRole(String primaryKey,HttpServletRequest request)  {
    	String hql = "";
    	if(primaryKey.equals("all")){
    		hql = "from HzsysRoleinfo order by roleorder asc";
    	}else{
    		hql = "from HzsysRoleinfo where roleid = '"+ primaryKey+"'  order by roleorder asc";
    	}
    	request.setAttribute("list", this.selHqlList(hql));
    }
    /**
     * 查询机构根节点
     * @author 
     * @time Sep 28, 2010	
     * @return 
     * 方法功能:
     */
    public List queryRootList(){
    	String hql="from HzsysPrograma where parentid =-1";
    	System.out.println("queryRootList:"+hql);
    	return this.selHqlList(hql);
    }
    /**
     * 模糊查询 
     * @param nodeName
     * @return
     */
    public List queryLikeNodeName(String depname,String pt){
    	String hql="from HzsysPrograma where proname like '%" + depname + "%'";
    	if(!"".equals(pt)){
    		hql += " and url like '%"+pt+"%'";
    	}
    	return this.selHqlList(hql);
    }
    /**
     * 根据唯一depid查询HzsysPrograma相关信息
     * @author 
     * @time Sep 28, 2010	
     * @param depid
     * @return 
     * 方法功能:
     */
    public HzsysPrograma queryNodeInfo(String proid){
    	String hql="from HzsysPrograma where proid = "+ proid;
    	HzsysPrograma dep = (HzsysPrograma) this.selHqlObj(hql);
    	System.out.println("queryNodeInfo:"+hql);
    	return dep;
    }
   
    /**
     * 查询对应子节点
     * @author 
     * @time Sep 28, 2010	
     * @param orgId
     * @return 
     * 方法功能:
     */
    public List getOrgDeptsSelect(String orgId)  {
    	String hql = "from HzsysPrograma where parentid='" + orgId + "' and depstate='" + Constants.NODE_STATE_ACTIVE + "'";
        System.out.println("getOrgDeptsSelect:"+hql);
        return this.selHqlList(hql);
    }
    /**
     * 生成岗位select
     * @author 
     * @time Sep 28, 2010	
     * @param orgId
     * @return 
     * 方法功能:
     */
    public List getProtPosSelect(String orgId)  {
    	String hql = "from HzsysPrograma where parentid='" + orgId + "' and depstate='" + Constants.NODE_STATE_ACTIVE + "'";
        System.out.println("getDeptPosSelect:"+hql);
        return this.selHqlList(hql);
    }
    
    /**
     * 编写xml信息
     * @author 
     * @time Sep 28, 2010	
     * @param dep
     * @return 
     * 方法功能:
     */
    public String generalTMIXml(HzsysPrograma pro){
    	StringBuffer sbf= new StringBuffer();
    	String parentid=pro.getParentid()==null?"":pro.getParentid().toString();
    	String proname=pro.getProname()==null?"":pro.getProname();
    	String procontent=pro.getProcontent()==null?"":pro.getProcontent();
    	String prosoncontent=pro.getProsoncontent()==null?"":pro.getProsoncontent();
    	String proorder=pro.getProorder()==null?"":pro.getProorder().toString();
    	String pronumber=pro.getPronumber()==null?"":pro.getPronumber();
    	String proid=pro.getProid()==null?"":pro.getProid().toString();
    	sbf.append("<proid><![CDATA["+proid+"]]></proid>");
    	sbf.append("<parentid><![CDATA["+parentid+"]]></parentid>");
    	sbf.append("<proname><![CDATA["+proname+"]]></proname>");
    	sbf.append("<procontent><![CDATA["+procontent+"]]></procontent>");
    	sbf.append("<prosoncontent><![CDATA["+prosoncontent+"]]></prosoncontent>");
    	sbf.append("<proorder><![CDATA["+proorder+"]]></proorder>");
    	sbf.append("<pronumber><![CDATA["+pronumber+"]]></pronumber>");
    	return sbf.toString();
    }
}
